package uit.nfcmenu.waiter.activities;

import java.io.Serializable;
import java.util.ArrayList;

import uit.nfcmenu.models.*;
import uit.nfcmenu.utils.ImageDownloader;
import uit.nfcmenu.waiter.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class ListItemsActivity extends Activity implements OnClickListener, IDataChangeHandler, Serializable  {
	public static String tag = ListItemsActivity.class.getSimpleName();

	// hard 8 // TODO dynamic menu-items numbers in vertical-scroll
	private ImageView[] mMenus;
	private TextView[] mDesc;
	private TextView txtTile;
	private Button bntOrderList, bntLogout, bntMenu;
	
	private int mainId;
	private String mainName;
	private DatabaseManager mDBManager;
	private ArrayList<ItemModel> arrItems;
	
	private ViewFlipper vf;
	private float oldTouchValue;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		initData();
		initView();
		registerEventListener();
		viewImages(); 
	}

	private void initData() {
		// get data for views
		mainId = getIntent().getExtras().getInt("mainId");
		mainName = getIntent().getExtras().getString("mainName");
		mDBManager = DatabaseManager.getInstance(this);
		// get sub menu by main and view to the screen
		arrItems = mDBManager.getItemsByMenu(mainId);
	}

	private void initView() {
		// TODO Auto-generated method stub
		mMenus = new ImageView[8];
		mMenus[0] = (ImageView) findViewById(R.id.img_detail_menu1);
		mMenus[1] = (ImageView) findViewById(R.id.img_detail_menu2);
		mMenus[2] = (ImageView) findViewById(R.id.img_detail_menu3);
		mMenus[3] = (ImageView) findViewById(R.id.img_detail_menu4);
		mMenus[4] = (ImageView) findViewById(R.id.img_detail_menu5);
		mMenus[5] = (ImageView) findViewById(R.id.img_detail_menu6);
		mMenus[6] = (ImageView) findViewById(R.id.img_detail_menu7);
		mMenus[7] = (ImageView) findViewById(R.id.img_detail_menu8);
		
		mDesc = new TextView[8];
		mDesc[0] = (TextView) findViewById(R.id.text_in_image1);
		mDesc[1] = (TextView) findViewById(R.id.text_in_image2);
		mDesc[2] = (TextView) findViewById(R.id.text_in_image3);
		mDesc[3] = (TextView) findViewById(R.id.text_in_image4);
		mDesc[4] = (TextView) findViewById(R.id.text_in_image5);
		mDesc[5] = (TextView) findViewById(R.id.text_in_image6);
		mDesc[6] = (TextView) findViewById(R.id.text_in_image7);
		mDesc[7] = (TextView) findViewById(R.id.text_in_image8);
		
		bntOrderList = (Button) findViewById(R.id.btnOrderList);
		bntLogout = (Button) findViewById(R.id.btnLogout);
		bntMenu = (Button) findViewById(R.id.btnMenu);
		txtTile = (TextView) findViewById(R.id.title_list);
	}

	private void viewImages() {	
		// show title
		txtTile.setText(mainName);
		// show list
		int n = (arrItems.size() > mMenus.length) ? mMenus.length : arrItems.size();
		ImageDownloader ima = new ImageDownloader();
		for (int i = 0; i < n; i++) {
			ima.download(arrItems.get(i).getImageLink(), mMenus[i]);
			mDesc[i].setText(""+arrItems.get(i).getItemName());
			//Log.i(tag, "view sub "+arrItems.get(i).toString());
		}
		
	}

	private void registerEventListener() {
		// TODO Auto-generated method stub
		mMenus[0].setOnClickListener(this);
		mMenus[1].setOnClickListener(this);
		mMenus[2].setOnClickListener(this);
		mMenus[3].setOnClickListener(this);
		mMenus[4].setOnClickListener(this);
		mMenus[5].setOnClickListener(this);
		mMenus[6].setOnClickListener(this);
		mMenus[7].setOnClickListener(this);
		
		int n = (arrItems.size() > mMenus.length) ? mMenus.length : arrItems.size();
		for (int i = 0; i < n; i++) {			
			final int index = i;
			mMenus[i].setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					launchDetailActivity(index);
				}
			});
		}
		
		bntOrderList.setOnClickListener(this);
		bntLogout.setOnClickListener(this);
		bntMenu.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnOrderList:
			launchOrderListActivity();
			break;
		case R.id.btnLogout:
			launchLoginActivity();
			break;	
		case R.id.btnMenu:
			launchMainMenuActivity();
			break;
		}
	}

	private void launchMainMenuActivity() {
		Intent intent = new Intent(this, MainMenuActivity.class);
		startActivity(intent);
	}

	private void launchLoginActivity() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}

	private void launchOrderListActivity() {
		Intent intent = new Intent(this, OrderListActivity.class);
		startActivity(intent);
	}

	private void launchDetailActivity(int index) {
		Intent intent = new Intent(this, DetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt("itemId", arrItems.get(index).getItemID());
		bundle.putString("itemUrl", arrItems.get(index).getImageLink());
		bundle.putInt("discount", arrItems.get(index).getDiscount());
		bundle.putString("description", arrItems.get(index).getDescription());
//		bundle.putString("itemUrl", arrItems.get(index).get);
//		bundle.putSerializable("item",(Serializable) arrItems.get(index) );
		intent.putExtras(bundle);
		startActivity(intent);
	}

	public void onDataChanged() {
		// TODO Auto-generated method stub
		
	}

	public Context getContext() {
		// TODO Auto-generated method stub
		return getBaseContext();
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValue = touchevent.getX();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                //if(this.searchOk==false) return false;
                float currentX = touchevent.getX();
                if (oldTouchValue < currentX)
                {
                	             	
                    vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
                    vf.setOutAnimation(AnimationHelper.outToRightAnimation());
                    
                                    
                    vf.showNext();
//                    if(radio1.isChecked()==true){
//                    	radio1.setChecked(false);
//                    	radio2.setChecked(true);
//                    }
//                    else if(radio2.isChecked()==true){
//                    	radio2.setChecked(false);
//                    	radio3.setChecked(true);
//                    }
//                    else if(radio3.isChecked()==true){
//                    	radio3.setChecked(false);
//                    	radio1.setChecked(true);
//                    }
                }
                if (oldTouchValue > currentX)
                {
                    vf.setInAnimation(AnimationHelper.inFromRightAnimation());
                    vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
                    vf.showPrevious();
//                    if(radio1.isChecked()==true){
//                    	radio1.setChecked(false);
//                    	radio3.setChecked(true);
//                    }
//                    else if(radio2.isChecked()==true){
//                    	radio2.setChecked(false);
//                    	radio1.setChecked(true);
//                    }
//                    else if(radio3.isChecked()==true){
//                    	radio3.setChecked(false);
//                    	radio2.setChecked(true);
//                    }
                }
            break;
            }
        }
        return false;
    }
	
	// ========================
	
}
